package wiki.zimo.dao;

import org.apache.commons.dbutils.handlers.BeanHandler;
import wiki.zimo.domain.User;

/**
 * @version v1.0
 * @auther: 子墨
 * @datetime: 2018/10/31 15:14
 * @description: User数据库操作类
 */
public class UserDao extends BaseDao<User> {

    /**
     * @auther: 子墨
     * @date: 2018/10/31 16:26
     * @describe: 用户登录, 验证注册重复使用
     * @param: [params]
     * @return: wiki.zimo.domain.User
     * @version v1.0
     */
    @Override
    public User findOneByParams(Object... params) throws Exception {
        String sql = null;
        if (params.length == 1) {
            sql = "select * from user where telephone = ? limit 1";
            return qr.query(sql, new BeanHandler<>(User.class), params[0]);
        } else if (params.length == 2) {
            sql = "select * from user where telephone = ? and password = ? limit 1";
            return qr.query(sql, new BeanHandler<>(User.class), params[0], params[1]);
        }
        return null;
    }
}
/*public class UserDao implements Dao<User,String>{

	// 获取SQL语句的内部类
	class SQL extends SQLUtils<User> {

	}
	// 获取SQL语句的对象
	SQL s = new SQL();

	@Override
	public User insert(User entity) throws SQLException {
//		String sql = "insert into User values(? , ? , ? , ?, ?, ?, ?, ? ,?, ?)";
		String sql = s.getInsertSql();
		return qr.insert(sql,
				new BeanHandler<>(User.class),
				entity.getUid(),
				entity.getUsername(),
				entity.getPassword(),
				entity.getName(),
				entity.getEmail(),
				entity.getTelephone(),
				entity.getBirthday(),
				entity.getSex(),
				entity.getState(),
				entity.getCode(),
				entity.getIsadmin()
		);
	}

	@Override
	public boolean delete(User entity) throws SQLException {
//		String sql = "delete from User where uid = ?";
		String sql = s.getDeleteSql();
		int update = qr.update(sql,
				entity.getUid()
		);
		if (update > 0){
			return true;
		}
		return false;
	}

	@Override
	public User update(User entity) throws SQLException {
//		String sql = "update User set username = ?, password = ?, name = ?, email = ?, telephone = ?,birthday = ?, sex = ?, state = ?, code = ? where uid = ?";
		String sql = s.getUpdateSql();
		int update = qr.update(sql,
				entity.getUsername(),
				entity.getPassword(),
				entity.getName(),
				entity.getEmail(),
				entity.getTelephone(),
				entity.getBirthday(),
				entity.getSex(),
				entity.getState(),
				entity.getCode(),
				entity.getIsadmin(),
				entity.getUid()
		);
		if (update > 0){
			return findOneById(entity.getUid());
		}
		return null;
	}

	@Override
	public User findOneById(String id) throws SQLException {
//		String sql = "select * from User where uid = ? limit 1";
		String sql = s.getFindOneByIdSql();
		return qr.query(sql, new BeanHandler<>(User.class), id);
	}

	*//**
 * @auther: 子墨
 * @date: 2018/10/31 16:26
 * @describe: 用户登录, 验证注册重复使用
 * @param: [params]
 * @return: wiki.zimo.domain.User
 * @version v1.0
 *//*
	@Override
	public User findOneByParams(String... params) throws SQLException {
		String sql = null;
		if (params.length == 1){
			sql = "select * from user where telephone = ? limit 1";
			return qr.query(sql, new BeanHandler<>(User.class), params[0]);
		}else if (params.length == 2){
			sql = "select * from user where telephone = ? and password = ? limit 1";
			return qr.query(sql, new BeanHandler<>(User.class), params[0], params[1]);
		}
		return null;
	}

	@Override
	public List<User> findAll() throws SQLException {
//		String sql = "select * from User";
		String sql = s.getFindAllSql();
		return qr.query(sql,new BeanListHandler<>(User.class));
	}

	@Override
	public int count() throws SQLException {
		return findAll().size();
	}
}*/
